var GenerateCode = {
  config: {
  },
  init: function (config) {
    var _this = this;
    $.extend(_this.config, config);
    Layout.set_menu_select(_this.config.menu_id);


    loading();
    _this.init_btn();
    _this.init_grid();

    sessionStorage.removeItem("scroll_top");
  },
  init_btn: function () {
    var _this = this;

    //查询按钮
    $(document).on('click', '#search_btn', function (e) {
      e.preventDefault();
      var query_param = $('#data_table').TableGrid('getparam');
      query_param.tablename = $('#tablename').val();
      _this.config.tablename = $('#tablename').val() ; 
      $('#data_table').TableGrid('param', query_param);
      $('#data_table').TableGrid('reload');
    });

    //打开生成model窗口
    $(document).on('click', '#load_add_model_btn', function (e) {
      e.preventDefault();
      var url = _this.config.get_model_info_url ;
      var data = { tablename : _this.config.tablename };

      $.post( url , data , function(ret){
        fillform( $('#model_form') , ret );
        $('#model_modal').modal('show');
      }) ;
    });
    
    //生成model
    $(document).on('click', '#add_model_btn', function (e) {
      e.preventDefault();
      var url = _this.config.add_model_url ;
      var data = $('#model_form').serializeObject()

      $.post( url , data , function(ret){
        tips(ret.info);
        $('#model_modal').modal('hide');
      }) ;
    });
    
    //打开生成service窗口
    $(document).on('click', '#load_add_service_btn', function (e) {
      e.preventDefault();

      var select_data = new Array();
      $("input[name='tablegrid-select[]']").each(function (index) {
        if ($(this).prop('checked')) {
          //取表数据
          var row = $('#data_table').TableGrid('getrow', index);
          select_data.push({
            field_name : row.field_name ,
            field_default : row.field_default
          });
        }
      });

      if (select_data.length == 0) {
        tips('请选择字段及默认值');
        return ;
      }

      _this.config.defaultrow = select_data ;
      var url = _this.config.get_service_info_url ;
      var data = { 
        tablename : _this.config.tablename ,
      };

      $.post( url , data , function(ret){
        fillform( $('#service_form') , ret );
        $('#service_modal').modal('show');
      }) ;
    });
    
    //生成model
    $(document).on('click', '#add_service_btn', function (e) {
      e.preventDefault();
      var url = _this.config.add_service_url ;
      var data = $('#service_form').serializeObject()
      data.defaultrow = _this.config.defaultrow ;
      $.post( url , data , function(ret){
        tips(ret.info);
        $('#service_modal').modal('hide');
      }) ;
    });
    
    //打开生成controller窗口
    $(document).on('click', '#load_add_controller_btn', function (e) {
      e.preventDefault();
      var url = _this.config.get_controller_info_url ;
      var data = { tablename : _this.config.tablename };

      $.post( url , data , function(ret){
        fillform( $('#controller_form') , ret );
        $('#controller_modal').modal('show');
      }) ;
    });
    
    //生成controller
    $(document).on('click', '#add_controller_btn', function (e) {
      e.preventDefault();
      var url = _this.config.add_controller_url ;
      var data = $('#controller_form').serializeObject()

      $.post( url , data , function(ret){
        tips(ret.info);
        $('#controller_modal').modal('hide');
      }) ;
    });
    
    //打开生成 js 窗口
    $(document).on('click', '.load_add_js_btn', function (e) {
      e.preventDefault();
      var url = _this.config.get_js_info_url ;
      var data = { 
        tablename : _this.config.tablename,
        type : $(this).data('type'),
      };

      $.post( url , data , function(ret){
        fillform( $('#js_form') , ret );
        $('#js_modal').modal('show');
      }) ;
    });
    
    //生成js
    $(document).on('click', '#add_js_btn', function (e) {
      e.preventDefault();
      var url = _this.config.add_js_url ;
      var data = $('#js_form').serializeObject()

      $.post( url , data , function(ret){
        tips(ret.info);
        $('#js_modal').modal('hide');
      }) ;
    });
    
    //打开生成 view 窗口
    $(document).on('click', '.load_add_view_btn', function (e) {
      e.preventDefault();
      
      var select_data = new Array();
      $("input[name='tablegrid-select[]']").each(function (index) {
        if ($(this).prop('checked')) {
          //取表数据
          var row = $('#data_table').TableGrid('getrow', index);
          select_data.push({
            field_name : row.field_name ,
            field_text : $('#field_text_' + row.field_name ).val(),
            field_width : $('#field_width_' + row.field_name ).val(),
            field_sort : $('#field_sort_' + row.field_name ).val(),
            field_format : $('#field_format_' + row.field_name ).val(),
          });
        }
      });

      if (select_data.length == 0) {
        tips('请选择字段及默认值');
        return ;
      }

      _this.config.select_row = select_data ;
      
      var url = _this.config.get_view_info_url ;
      var data = { 
        tablename : _this.config.tablename ,
        type      : $(this).data('type'),
      };

      $.post( url , data , function(ret){
        fillform( $('#view_form') , ret );
        $('#view_modal').modal('show');
      }) ;
    });
    
    //生成view
    $(document).on('click', '#add_view_btn', function (e) {
      e.preventDefault();
      var url = _this.config.add_view_url ;
      var data = $('#view_form').serializeObject()
      data.select_row = _this.config.select_row ;
      $.post( url , data , function(ret){
        tips(ret.info);
        $('#view_modal').modal('hide');
      }) ;
    });
  },
  init_grid: function () {
    var _this = this;
    $('#data_table').TableGrid({
      url: _this.config.get_tableinfo_url ,
      select_all: true,
      load_after_init: false,
      pagination: false ,
      after_load : function() {
        _this.config.tabledata = $('#data_table').TableGrid('getrows');
        if ( _this.config.tabledata.length == 0 ) {
          tips('表未找到！');
        } else {
          $('#add_btn').show();
        }
      }
    });
  }
};

var format_default = function( value, row, index ){
  return '<input id="field_default_'+ index +'" value="'+ value +'" class="form-control" >';
}

var format_text = function( value, row, index ){
  return '<input id="field_text_'+ row.field_name +'" value="'+ row.field_comment +'" class="form-control" >';
}

var format_width = function( value, row, index ){
  return '<input id="field_width_'+ row.field_name +'" value="80" class="form-control" >';
}

var format_sort = function( value, row, index ){
  var html = '<select id="field_sort_'+ row.field_name +'" class="form-control" >' ;
  html += '<option value="0" selected>否</option>';
  html += '<option value="1">是</option>';
  html += '</select>';
  return html;
}

var format_format = function( value, row, index ){
  return '<input id="field_format_'+ row.field_name +'" value="" class="form-control" >';
}



 
